#include "../Base/stdafx.h"
#include "../Base/DxDevice.h"
#include "Sprite.h"
#include "../Base/GameEngine.h"

Sprite::Sprite(const tstring& file, const D3DXVECTOR3& pos, int count): m_TexturePtr(0), 
	m_ResourcePtr(0), m_Width(0), m_Height(0), m_CurrStep(0), m_MaxStep(count), m_OldTime(0)
{
	m_TexturePtr = GetTextureFromFile(file);
	if(!m_TexturePtr) LOG(tstring(_T("Kan ") + file + _T(" niet inladen")));
	CreateResourceView(m_TexturePtr, &m_ResourcePtr);
	//D3DX10CreateShaderResourceViewFromFile(DXDevice::GetSingleton()->GetDevice(), file.c_str(), NULL, NULL, &m_ResourcePtr, NULL);
	
	m_OrigPos = pos;

	m_Pos = pos;

	m_Pos.x += m_Width / 2;
	m_Pos.y = GameEngine::GetSingleton()->GetHeight() - (pos.y + m_Height/2);

	//m_TexturePtr->Release();

	m_Sprite.ColorModulate = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
	m_Sprite.pTexture = m_ResourcePtr;
	m_Sprite.TexCoord.x = 0;
	m_Sprite.TexCoord.y = 0;
	m_Sprite.TexSize.x = 1 / (float)count;
	m_Sprite.TexSize.y = 1;
	m_Sprite.TextureIndex = 0;
	D3DXMatrixScaling(&m_Scale, GetWidth(), GetHeight(), 1.0f);
	D3DXMatrixTranslation(&m_Tran, m_Pos.x, m_Pos.y, m_Pos.z);
	m_World = m_Scale * m_Tran;
	m_Sprite.matWorld = m_World;

}

Sprite::~Sprite()
{

}

void Sprite::SetPosition(const D3DXVECTOR3& pos)
{
	m_OrigPos = pos;
	D3DXVECTOR3 pos2 = pos;
	pos2.x += (m_Width / 2);
	pos2.y = GameEngine::GetSingleton()->GetHeight() - (pos.y + m_Height/2);

	m_Pos = pos2;

	D3DXMatrixTranslation(&m_Tran, m_Pos.x, m_Pos.y, m_Pos.z);
	Update();

}

const D3DXVECTOR3& Sprite::GetPosition() const
{
	return m_Pos;
}

D3DX10_SPRITE Sprite::GetSprite() const
{
	return m_Sprite;
}

ID3D10Texture2D* Sprite::GetTextureFromFile(const tstring& file) const
{
	ID3D10Resource* res;
	HRESULT hr = D3DX10CreateTextureFromFile(DXDevice::GetSingleton()->GetDevice(), file.c_str(), NULL, NULL, &res, NULL);
	if(FAILED(hr))
		return NULL;
	else
		return (ID3D10Texture2D*)res;
}

void Sprite::CreateResourceView(ID3D10Texture2D* texture, ID3D10ShaderResourceView ** view)
{
	if(texture != NULL)
	{
		D3D10_TEXTURE2D_DESC desc;
		texture->GetDesc(&desc);

		m_Width = (float)desc.Width / m_MaxStep;
		m_Height = (float)desc.Height;

		D3D10_SHADER_RESOURCE_VIEW_DESC viewDesc;
		ZeroMemory(&viewDesc, sizeof(viewDesc));
		viewDesc.Format = desc.Format;
		viewDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
		viewDesc.Texture2D.MipLevels = desc.MipLevels;

		DXDevice::GetSingleton()->GetDevice()->CreateShaderResourceView(texture, &viewDesc, view);
	}
}

float Sprite::GetHeight() const
{
	return m_Height;
}

float Sprite::GetWidth() const 
{
	return m_Width;
}

void Sprite::Tick(float dTime)
{
	m_OldTime += dTime;
	if(m_OldTime >= 0.1f)
	{
		m_OldTime = 0;
		++m_CurrStep;
		if(m_CurrStep == m_MaxStep)
			m_CurrStep = 0;
	}

	m_Sprite.TexCoord.x = m_CurrStep * (1/(float)m_MaxStep);
	//m_Sprite.TexSize.x = m_Sprite.TexCoord.x + m_Width;
}

void Sprite::SetScale(const D3DXVECTOR3& scale)
{
	m_Width *= scale.x;
	m_Height *= scale.y;
	D3DXMATRIX matScale;
	D3DXMatrixScaling(&matScale, scale.x, scale.y, scale.z);
	m_Scale *= matScale;
	SetPosition(m_OrigPos);
	Update();
}

void Sprite::Update()
{
	m_World = m_Scale * m_Tran;
	m_Sprite.matWorld = m_World;
}

void Sprite::SetStep(int step)
{
	if(step > m_MaxStep)
		m_CurrStep = m_MaxStep;
	else
		m_CurrStep = step;

	m_Sprite.TexCoord.x = m_CurrStep * (1/(float)m_MaxStep);
}